using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.TextMining.Properties;

namespace Workflows.Components.TextMining.IFilter
{
    [Serializable]
    public class FileContentSearchComponent:WorkflowComponentBase
    {
        private FileContentSearchInstruction _Instruction;
        private FileContentSearchOutput _Output;

        public FileContentSearchComponent():base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.TextMining.ToString();
            this.Description = "search files for pattern within a folder";
            this.ComponentIcon = Resources.vdt80pui_1050;
            this.ComponentImage = Resources.Journal2;
            this.ComponentName = "File filter";

            this._Instruction =
                new FileContentSearchInstruction(this.ComponentID, Guid.Empty, null, string.Empty,
                                                 SearchOption.TopDirectoryOnly, StringMatchType.ExactMatch);
            this._Output = new FileContentSearchOutput(this.ComponentID, Guid.Empty,null);
        }

        #region props
        public List<string> SourceFilePaths
        {
            get { return this._Instruction.SourceFilePaths; }
            set { this._Instruction.SourceFilePaths = value; }
        }

        public SearchOption SearchOption
        {
            get { return this._Instruction.SearchOption; }
            set { this._Instruction.SearchOption = value; }
        }

        public string FileNamePattern
        {
            get { return this._Instruction.FileNamePattern; }
            set { this._Instruction.FileNamePattern = value; }
        }

        public override InstructionBase Instruction
        {
            get { return _Instruction; }
            set { _Instruction =(FileContentSearchInstruction) value; }
        }

        public override ResultBase Output
        {
            get { return _Output; }
            set { _Output =(FileContentSearchOutput) value; }
        }

        public StringMatchType StringMatchOption
        {
            get { return this._Instruction.StringMatchOption; }
            set { this._Instruction.StringMatchOption = value; }
        }
        #endregion

        

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileNamePattern", this.FileNamePattern);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SearchOption", ((int) this.SearchOption).ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StringMatchOption", ((int) this.StringMatchOption).ToString());

            XmlNode srcFileRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "SourceFiles");
            if (this.SourceFilePaths != null && this.SourceFilePaths.Count > 0)
            {
                foreach (string filePath in this.SourceFilePaths)
                {
                    XmlNode fileNode = XmlDataUtil.AddElement(ref xDoc, srcFileRootNode, "File");
                    XmlDataUtil.UpdateAttribute(ref xDoc, fileNode, "FilePath", filePath);
                }
            }

            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FileContentSearchComponent wfComponent = base.Instantiate(xNode) as FileContentSearchComponent;

                wfComponent.FileNamePattern = xNode.Attributes.GetNamedItem("FileNamePattern").Value;
                wfComponent.SearchOption = (SearchOption) int.Parse(xNode.Attributes.GetNamedItem("SearchOption").Value);
                wfComponent.StringMatchOption =
                    (StringMatchType) int.Parse(xNode.Attributes.GetNamedItem("StringMatchOption").Value);

                wfComponent.SourceFilePaths = null;
                XmlNodeList fileNodes = xNode.SelectNodes("SourceFiles/File");
                if (fileNodes != null && fileNodes.Count > 0)
                {
                    wfComponent.SourceFilePaths = new List<string>();
                    foreach (XmlNode fileNode in fileNodes)
                    {
                        string filePath = fileNode.Attributes.GetNamedItem("FilePath").Value;
                        wfComponent.SourceFilePaths.Add(filePath);
                    }
                }

                return wfComponent;
            }
            else
            {
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
            }
        }
    }
}